<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-home"></i> 用户管理</el-breadcrumb-item>
                <el-breadcrumb-item>黑名单管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-form ref="form" :inline="true" :model="form">
                    <el-form-item>
                        <el-input
                            clearable
                            @keyup.enter.native="handleSearch"
                            v-model="query.userName"
                            placeholder="用户名"
                            class="handle-input mr10"
                        ></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-input
                            clearable
                            @keyup.enter.native="handleSearch"
                            v-model="query.uid"
                            placeholder="用户名ID"
                            class="handle-input mr10"
                        ></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-input
                            clearable
                            @keyup.enter.native="handleSearch"
                            v-model="query.phone"
                            placeholder="手机号"
                            class="handle-input mr10"
                        ></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleSearch">搜索</el-button>
                    </el-form-item>
                    <!-- <el-button style="float:right;" type="primary" @click="handleBlack">添加黑名单</el-button> -->
                </el-form>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column label="用户ID" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.uid }}</template>
                </el-table-column>
                <el-table-column label="用户名" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.userName }}</template>
                </el-table-column>
                <el-table-column label="手机" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.phone }}</template>
                </el-table-column>
                <!-- <el-table-column label="手机" align="center">
                    <template slot-scope="scope">{{ scope.row.name }}</template>
                </el-table-column> -->
                <el-table-column label="邮箱" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.email }}</template>
                </el-table-column>

                <el-table-column label="拉黑时间" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.limitTime }}</template>
                </el-table-column>

                <el-table-column label="原因" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.limitReason }}</template>
                </el-table-column>

                <el-table-column label="备注" align="center" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{ scope.row.mark }}</template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <!-- <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button> -->
                        <el-button type="text" class="red" @click="handleDeleteBlack(scope.$index, scope.row)">移除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total,sizes, prev, pager, next, jumper"
                    :page-sizes="[5, 10, 20, 30]"
                    @size-change="handleSizeChange"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 添加黑名单 -->
        <el-dialog title="添加黑名单" :visible.sync="editVisible" width="50%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.userName" clearable></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="form.phone" clearable></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.email" clearable></el-input>
                </el-form-item>
                <el-form-item label="原因">
                    <el-select v-model="form.limitReason" placeholder="" :clearable="true" class="handle-select">
                        <el-option key="0" label="恶意投诉" :value="0"></el-option>
                        <el-option key="1" label="频繁发布广告" :value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="备注">
                    <el-input clearable v-model="form.mark" placeholder="备注信息"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="saveEdit">确 定</el-button>
                <el-button @click="editVisible = false">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '@/api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                // address: '',
                userName: '',
                uid: '',
                phone: '',
                pageIndex: 0,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {
                userName: '',
                phone: '',
                email: '',
                limitReason: '',
                mark: ''
            },
            // idx: -1,
            id: '',
            uid: ''
        };
    },
    activated() {
        this.getData();
    },
    methods: {
        async getData() {
            try {
                const url = '/user/userblock/list';
                const { data: res } = await this.$http[0].get(url, {
                    params: {
                        page: this.query.pageIndex,
                        size: this.query.pageSize,
                        userName: this.query.userName,
                        uid: this.query.uid,
                        phone: this.query.phone
                    }
                });
                console.log(res);

                if (res.data.content && res.data.content.length > 0) {
                    res.data.content.map(item => {
                        item.limitTime = this.$dateFormat(item.limitTime, '', '-');
                    });
                    this.tableData = res.data.content;
                    this.pageTotal = res.data.totalElements || 0;
                } else {
                    this.tableData = [];
                    this.pageTotal = 0;
                }
            } catch (err) {
                console.dir(err);
                this.$message.error({
                    message: err.message,
                    duration: 1500
                });
            }
            // fetchData(this.query).then(res => {
            //     console.log(res);
            //     this.tableData = res.list;
            //     this.pageTotal = res.pageTotal || 50;
            // });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 0);
            this.getData();
        },

        // handleBlack() {
        //     this.editVisible = true;
        // },
        // 删除操作
        handleDeleteBlack(index, row) {
            console.log(index, row);
            this.uid = row.uid;
            this.id = row.id;
            this.editVisible = true;
            // 二次确认删除
            // this.$confirm('确定要删除吗？', '提示', {
            //     type: 'warning'
            // })
            //     .then(() => {
            //         this.$message.success('删除成功');
            //         this.tableData.splice(index, 1);
            //     })
            //     .catch(() => {});
        },
        async saveEdit() {
            try {
                const url = '/user/remove/userblock';
                const { data: res } = await this.$http[0].post(url, `uid=${this.uid}&id=${this.id}&mark=${this.form.mark}`, {
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                });
                console.log(res);
                this.$message.success({
                    message: err.message,
                    duration: 1500
                });
                this.editVisible = false;
                this.getData();
            } catch (err) {
                console.dir(err);
                this.editVisible = false;
                this.$message.error({
                    message: err.message,
                    duration: 1500
                });
            }
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        // handleEdit(index, row) {
        //     this.idx = index;
        //     this.form = row;
        //     this.editVisible = true;
        // },
        // 保存编辑

        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        handleSizeChange(val) {
            this.$set(this.query, 'pageSize', val);
            this.getData();
        }
    }
};
</script>

<style scoped lang="scss">
.handle-box {
    margin-bottom: 20px;
    .el-form {
        display: grid;
        grid-auto-flow: column;
        justify-content: flex-start;
        .el-form-item {
            width: 100%;
            .el-form-item__content {
                .el-select,
                .handle-input {
                    width: 100%;
                }
            }
        }
    }
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
